<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增BOM')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-productbom-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">BOM编号：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="bomCode" name="bomCode" class="form-control" th:value="${bomCode}" type="text" readonly>
                        <span class="input-group-addon"><a href="javascript:refreshOrder()"><i class="fa fa-refresh"></i>刷新单号</a></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">生产商品：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input id="productId" name="productId" type="hidden">
                        <input id="productCode" name="productCode" type="hidden">
                        <input id="productName" name="productName" class="form-control" type="text"
                               onclick="selectMainGoods()" readonly required>
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">排产最小数量：</label>
                <div class="col-sm-8">
                    <input name="productMinNum" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">单位：</label>
                <div class="col-sm-8">
                    <input id="productUnit"   name="productUnit" class="form-control" type="hidden" >
                    <input id="productUnitDesc" name="productUnitDesc" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="remark" class="form-control" maxlength="200"></textarea>
                </div>
            </div>

            <h4 class="form-header h4">BOM物料数据</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>

        <div class="row">
            <div class="col-sm-offset-5 col-sm-10">
                <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
                <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "order/productbom"
        var goodsUnitDatas = [[${@dict.getType('product_unit')}]];//商品单位

        $("#form-productbom-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.saveTab(prefix + "/add", $('#form-productbom-add').serialize());
            }
        }

        function  refreshOrder() {
            var config = {
                url: prefix + "/getOrderNo",
                type: "GET",
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍后...");
                },
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $("#bomCode").val(result.data)
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $.modal.closeLoading();
                }

            };
            $.ajax(config);
        }

        /* 选择生产商品 */
        var goodsPrefix = ctx + "basic/goods"
        function  selectMainGoods() {
            var url = goodsPrefix + '/selectGoods';
            var options = {
                title: '选择商品',
                url: url,
                callBack: doSubmitMainGoods
            };
            $.modal.openOptions(options);
        }

        function doSubmitMainGoods(index, layero){
            var iframeWin = layero.find("iframe")[0].contentWindow.$.table;
            var rows = iframeWin.selectFirstColumns();//调用子页面的方法 获取ID
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }

            var goodsId=iframeWin.selectColumns("id").join();//调用子页面的方法 获取ID
            var goodsCode=iframeWin.selectColumns("goodsCode").join();//调用子页面的方法 获取ID
            var goodsName=iframeWin.selectColumns("goodsName").join();//调用子页面的方法 获取ID
            var goodsUnit=iframeWin.selectColumns("goodsUnit").join();//调用子页面的方法 获取ID
            var goodsUnitDesc = "";//单位描述
            $.each(goodsUnitDatas,function(index,obj){  //index:索引obj:循环的每个元素
                if(obj.dictValue == goodsUnit){
                    goodsUnitDesc= obj.dictLabel;
                }
            });
            $.modal.close(index);

            $('#productId').val(goodsId);
            $('#productCode').val(goodsCode);
            $('#productName').val(goodsName);
            $('#productUnit').val(goodsUnit);
            $('#productUnitDesc').val(goodsUnitDesc);

            //重置子表
            sub.removeAllRow();
        }

        $(function() {
            // 初始化数据, 可以由后台传过来
            var data = [];
            var options = {
                data: data,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'index',
                        align: 'center',
                        title: "序号",
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            return columnIndex + $.table.serialNumber(index);
                        }
                    },
                    {
                        field: 'id',
                        align: 'center',
                        title: 'id',
                        visible: false
                    },
                    {
                        field: 'goodsName',
                        align: 'center',
                        title: '商品名称',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='items[%s].goodsName' value='%s' readonly>", index, value);
                            var goodsId = $.common.sprintf("<input type='hidden' name='items[%s].goodsId' value='%s'>", index, row.goodsId);//商品id 隐藏域
                            var goodsCode = $.common.sprintf("<input type='hidden' name='items[%s].goodsCode' value='%s'>", index, row.goodsCode);//商品code 隐藏域
                            return html+goodsId+goodsCode;
                        },
                        events: onclickEvent
                    },
                    {
                        field: 'planNumber',
                        align: 'center',
                        title: '数量',
                        formatter: function(value, row, index) {
                            var html = '<input class="form-control" type="number" name="items['+index+'].planNumber" value="'+value+'" maxlength="6"  required>';
                            return html;
                        },
                    },
                    {
                        field: 'goodsUnitDesc',
                        align: 'center',
                        title: '单位',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' type='text' name='items[%s].goodsUnitDesc' value='%s' readonly>", index, value);
                            var goodsUnit = $.common.sprintf("<input type='hidden' name='items[%s].goodsUnit' value='%s'>", index, row.goodsUnit);//商品unit
                            return html+goodsUnit;
                        }
                    },
                    {
                        field: 'remark',
                        align: 'center',
                        title: '要求',
                        formatter: function(value, row, index) {
                            var html = '<textarea class="form-control"  name="items['+index+'].remark" maxlength="200">'+value+'</textarea>';
                            return html;
                        }
                    }
                ]
            };
            $.table.init(options);
        });

        //新增table行
        function addColumn() {
            var row = {
                id: "",
                goodsId: "",
                goodsName: "",
                goodsUnit: "",
                goodsUnitDesc:"",
                planNumber: "",
                remark: ""
            }
            sub.addRow(row);
        }

        /* 选择商品 */
        var goodsPrefix = ctx + "basic/goods"
        function  selectShopGoods() {// index:所在行的索引
            var url = goodsPrefix + '/selectGoods';
            var options = {
                title: '选择商品',
                url: url,
                callBack: doSubmitGoods
            };
            $.modal.openOptions(options);
        }
        var orderSeedId;//子表ID
        function doSubmitGoods(index, layero){
            var iframeWin = layero.find("iframe")[0].contentWindow.$.table;
            var rows = iframeWin.selectFirstColumns();//调用子页面的方法 获取ID
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }

            var goodsId=iframeWin.selectColumns("id").join();//调用子页面的方法 获取ID
            var goodsCode=iframeWin.selectColumns("goodsCode").join();//调用子页面的方法 获取ID
            var goodsName=iframeWin.selectColumns("goodsName").join();//调用子页面的方法 获取ID
            var goodsUnit=iframeWin.selectColumns("goodsUnit").join();//调用子页面的方法 获取ID
            var planNumber=iframeWin.selectColumns("planNumber").join();//调用子页面的方法 获取ID
            var goodsUnitDesc = "";//单位描述
            $.each(goodsUnitDatas,function(index,obj){  //index:索引obj:循环的每个元素
                if(obj.dictValue == goodsUnit){
                    goodsUnitDesc= obj.dictLabel;
                }
            });
            var data = $("#" + table.options.id).bootstrapTable('getData');
            var flag=0;
            $.each(data,function(index,obj) {  //index:索引obj:循环的每个元素
                if(obj.goodsId ==  goodsId){
                    flag=1;
                    return false; //跳出循环
                }
            });
            if(flag == 1){$.modal.alertWarning("商品已在清单中!"); return;}
            $.modal.close(index);

            var _data = new Object();//某行数据
            _data.goodsId = goodsId;
            _data.goodsCode = goodsCode;
            _data.goodsName = goodsName;
            _data.goodsUnit = goodsUnit;
            _data.goodsUnitDesc = goodsUnitDesc;
            _data.planNumber = planNumber;
            $("#" + table.options.id).bootstrapTable("updateRow",{index : orderSeedId,row : _data});
        }

        /**
         * 选中商品事件
         * */
        window.onclickEvent = {
            'click .form-control': function (e, value, row, index) {
                orderSeedId = index
                selectShopGoods();
            }
        };

    </script>
</body>
</html>